package com.gxa.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.commons.JsonResult;
import com.gxa.common.commons.R;
import com.gxa.common.utils.FileUtils;
import com.gxa.common.utils.RandomUtils;
import com.gxa.order.common.UploadController;
import com.gxa.order.mapper.ConsultMapper;
import com.gxa.order.pojo.Consult;
import com.gxa.order.service.ConsultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ConsultServiceImpl extends ServiceImpl<ConsultMapper,Consult> implements ConsultService {
    @Autowired
    private ConsultMapper consultMapper;

    @Override
    public Map findConsultList(Integer currentPage, Integer pageSize) {
        Map map=new HashMap();
        try{
            Integer start =(currentPage-1)*pageSize;
            Integer off = pageSize;
            Page<Consult> page =new Page(start,off);
            QueryWrapper queryWrapper =new QueryWrapper();
            queryWrapper.in("status",1,2);
            Page<Consult> consultPage = consultMapper.selectPage(page,queryWrapper);
            //总条数
            map.put("count",consultPage.getTotal());
            map.put("data",consultPage.getRecords());
            map.put("code",200);
            map.put("msg","查询成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",500);
            map.put("msg","网络开小差");
        }

        return map;
    }
    //录入添加
    @Override
    public R addConsult(Consult consult) {
        R r =new R();
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime = format.format(date);
        //后端录入
        consult.setOrderSource(2);
        //录入当前时间
        consult.setEntryTime(dateTime);
        consult.setPresetTime(dateTime);
        //生成预订编号
        String number = RandomUtils.generateNumberString(16);
        consult.setCleanOrderNumber(number);
        consult.setPrincipal("小张");
        consult.setStatus(1);
        Integer result =consultMapper.addConsult(consult);
        if (result > 0){
            r.setCode(200);
            r.setMsg("添加成功");
            r.setData(result);
        }else {
            r.setCode(500);
            r.setMsg("添加失败");
        }
        return r;
    }

    @Override
    public JsonResult findCommunicateList(Long currentPage, Long pageSize, Integer status) {
        JsonResult jr=new JsonResult();
        try{
            Long start =(currentPage-1)*pageSize;
            Long off = pageSize;
            Page<Consult> page =new Page(start,off);
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("status",1);
            Page<Consult> consultPage=consultMapper.selectPage(page,queryWrapper);
            jr.setCode(200);
            jr.setMsg("查询成功");
            jr.setCount((int) consultPage.getTotal());
            jr.setData(consultPage.getRecords());
        }catch (Exception e){
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("网络炸了");
        }
        return jr;
    }

    @Override
    public JsonResult findCentreList(Long currentPage, Long pageSize, Integer status) {
        JsonResult jr=new JsonResult();
        try{
            Long start =(currentPage-1)*pageSize;
            Long off = pageSize;
            Page<Consult> page =new Page(start,off);
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("status",2);
            Page<Consult> consultPage=consultMapper.selectPage(page,queryWrapper);
            jr.setCode(200);
            jr.setMsg("查询成功");
            jr.setCount((int) consultPage.getTotal());
            jr.setData(consultPage.getRecords());
        }catch (Exception e){
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("网络炸了");
        }
        return jr;
    }

    @Override
    public JsonResult findThreadList(Long currentPage, Long pageSize, Integer status) {
        JsonResult jr=new JsonResult();
        try{
            Long start =(currentPage-1)*pageSize;
            Long off = pageSize;
            Page<Consult> page =new Page(start,off);
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("status",3);
            Page<Consult> consultPage=consultMapper.selectPage(page,queryWrapper);
            jr.setCode(200);
            jr.setMsg("查询成功");
            jr.setCount((int) consultPage.getTotal());
            jr.setData(consultPage.getRecords());
        }catch (Exception e){
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("网络炸了");
        }
        return jr;
    }

    @Override
    public Map updateClaimByStatus(Consult consult) {
        Map map =new HashMap();
        try{
            int result = consultMapper.updateClaimByStatus(consult);
            if (result>0){
                map.put("code",200);
                map.put("msg","修改成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",500);
            map.put("msg","失败");
        }
        return map;
    }

    @Override
    public JsonResult queryConsult(Integer currentPage,Integer pageSize,String condition) {
        JsonResult jr=new JsonResult();
        try{
            Integer totalSize=consultMapper.CountCondition(condition);
            int start =(currentPage-1)*pageSize;
            int off =pageSize;
            List<Consult> consultList = consultMapper.queryConsult(start, off, condition);
            jr.setCode(200);
            jr.setMsg("查询成功");
            jr.setCount(totalSize);
            jr.setData(consultList);
        }catch (Exception e){
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("失败");
        }

        return jr;
    }

    @Override
    public JsonResult queryConsultf(Integer currentPage, Integer pageSize, Integer typeOfService) {
        JsonResult jr=new JsonResult();
        try{
            int totalSize = consultMapper.CountTypeOfService(typeOfService);
            int start =(currentPage-1)*pageSize;
            int off =pageSize;
            List<Consult> consultList = consultMapper.queryConsultf(start, off, typeOfService);
            jr.setCount(totalSize);
            jr.setCode(200);
            jr.setMsg("查询成功");
            jr.setData(consultList);
        }catch (Exception e){
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("失败");
        }

        return jr;
    }

    @Override
    public R updateContent(Consult consult) {
        R r =new R();
        int result = consultMapper.updateById(consult);
        if (result>0){
            r.setCode(200);
            r.setMsg("添加成功");
        }else {
            r.setCode(500);
            r.setMsg("失败");
        }
        return r;
    }

    @Override
    public R updateCounselors(HashMap map) {
            R r = new R();
        System.out.println(map.toString());
        List a = (ArrayList)map.get("a");
        String b = map.get("b").toString();
        int i = consultMapper.updateCounselors(a, b);
        if (i > 0 ){
            r.setCode(200);
            r.setMsg("修改成功");
        }else {
            r.setCode(500);
            r.setMsg("失败");
        }
        return r;
    }
    @Override
    public R addAppContent(Consult consult) {
        R r =new R();
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime = format.format(date);
        consult.setPresetTime(dateTime);
        consult.setOrderSource(1);
        String number = RandomUtils.generateNumberString(16);
        consult.setCleanOrderNumber(number);
        consult.setPrincipal("小王");
        consult.setServiceName(1);
        consult.setTypeOfService(2);
        consult.setCountryName("中国");
        consult.setStatus(1);
        int result = consultMapper.addAppContent(consult);
        if (result>0){
            r.setCode(200);
            r.setMsg("录入成功");
        }else {
            r.setCode(500);
            r.setMsg("失败");
        }
        return r;
    }
    @Override
    public R updateStatusByClaim(Consult consult) {
        R r=new R();
        int result=consultMapper.updateStatusByClaim(consult);
        if (result>0){
            r.setCode(200);
            r.setMsg("认领成功");
        }else {
            r.setCode(500);
            r.setMsg("失败");
        }
        return r;
    }

    @Override
    public JsonResult findConsultById(Integer consult_id) {
        JsonResult jr=new JsonResult();
        try{
            Consult consult = consultMapper.selectById(consult_id);
            jr.setCode(200);
            jr.setMsg("查询成功");
            jr.setData(consult);
        }catch (Exception e){
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("失败");
        }

        return jr;
    }

    @Override
    public JsonResult findConsultByUserId(Integer appUserId) {
        JsonResult jr=new JsonResult();

        try{

        List<Consult> consult = consultMapper.findConsultByUserId(appUserId);
            System.out.println(consult);
            jr.setCode(200);
            jr.setMsg("查询成功");

            jr.setData(consult);
        }catch (Exception e){
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("失败");
        }
        return jr;
    }

}
